MongoDB - Text Search
Starting from version 2.4, MongoDB supports text indexes for searching inside string content. Text Search uses stemming techniques to search for specified words in string fields by dropping stemming stop words like "a," "an," "the," etc. MongoDB currently supports around 15 languages for text search.
Enabling Text Searchβ
Initially, Text Search was an experimental feature, but starting from version 2.6, it is enabled by default in MongoDB configurations.
Creating Text Indexβ
Consider the following documents in the posts
collection:
db.posts.insert({
"post_text": "enjoy the mongodb articles on tutorialspoint",
"tags": ["mongodb", "tutorialspoint"]
})
db.posts.insert({
"post_text": "writing tutorials on mongodb",
"tags": ["mongodb", "tutorial"]
})
To create a text index on the post_text
field, enabling text search inside the posts' text:
db.posts.createIndex({ post_text: "text" })
Using Text Indexβ
After creating the text index on the post_text
field, you can search for posts containing specific words using the $text
operator:
db.posts.find({ $text: { $search: "tutorialspoint" } }).pretty()
This command retrieves documents that contain the word "tutorialspoint" in their post text.
Diagram: Text Search Processβ
::: note: Performance Considerations
When using text search in MongoDB:
- Text indexes can significantly improve search performance for string content.
- Consider the language-specific settings for stemming and stop words.
- Optimize text indexes based on the search requirements. :::
Table: Text Search Optionsβ
Option | Description |
---|---|
$text | Operator for text search using text indexes. |
$search | Specifies the search term or phrase. |
$language | Optional parameter for specifying the language for text search. |
Deleting Text Indexβ
To delete an existing text index, first, find the name of the index using the getIndexes
method:
db.posts.getIndexes()
After obtaining the index name, use the dropIndex
method to delete the text index:
db.posts.dropIndex("post_text_text")
This command deletes the text index named "post_text_text" from the posts
collection.